﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

    class AssetMgr : SingletonAuto<AssetMgr>
    {
        readonly string _UIPath = null;

        public AssetMgr()
        {
#if UNITY_EDITOR
            _UIPath = "Assets/Code/Performance/UI/";
#else
            _UIPath = "Performance/UI/";
#endif
        }

        private UnityEngine.Object LoadUI(string prefabName)
        {
            return Load(_UIPath + prefabName);
        }

        private UnityEngine.Object Load(string path)
        {
#if UNITY_EDITOR
            Debug.Log(path.Remove(path.LastIndexOf(".")));
            return UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path);
#else

            return Resources.Load(path.Remove(path.LastIndexOf(".")));
#endif
        }


        /// <summary>
        /// 加载layout下的一个gui对象
        /// </summary>
        /// <param name="prefabName">对象对于的prefab文件</param>
        /// <param name="funcID">功能ID，默认为0，从layout文件夹取资源，如果非0，则从 7/funcID/layout/ 路径下取资源</param>
        /// <param name="parentObj">新的对象的父对象，可以传null</param>
        /// <returns>gui对象，加载失败就抛出异常</returns>
        public UnityEngine.GameObject LoadGuiObject(string prefabName, GameObject parentObj = null)
        {
            UnityEngine.Object obj = LoadUI(prefabName);
            UnityEngine.GameObject go = GameObject.Instantiate(obj) as UnityEngine.GameObject;
            if (parentObj != null)
                go.transform.SetParent(parentObj.transform);
            return go;
        }
    }
